#include "DeviceDeleteBtnDelegate.h"
#include <QDebug>
DeviceDeleteBtnDelegate::DeviceDeleteBtnDelegate(QObject *)
{

}

void DeviceDeleteBtnDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if (index.column() == 9) {
        QStyleOptionButton buttonOption;
        buttonOption.rect = option.rect;
        buttonOption.text = "删除";
        buttonOption.state = QStyle::State_Enabled;
        QApplication::style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter);
    } else if(index.column() == 10) {
        QStyleOptionButton buttonOption;
        buttonOption.rect = option.rect;
        buttonOption.text = "编辑";
        buttonOption.state = QStyle::State_Enabled;
        QApplication::style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter);
    }else if(index.column() == 11) {
        QStyleOptionButton buttonOption;
        buttonOption.rect = option.rect;
        buttonOption.text = "查看";
        buttonOption.state = QStyle::State_Enabled;
        QApplication::style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter);
    }
    else {
        QStyledItemDelegate::paint(painter, option, index);
    }
}

bool DeviceDeleteBtnDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    if (index.column() == 9 && event->type() == QEvent::MouseButtonRelease) {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
        if (option.rect.contains(mouseEvent->pos())) {
            MonitoringDeviceTableModel *deviceModel = static_cast<MonitoringDeviceTableModel *>(model);
            deviceModel->removeDevice(index.row());
            qDebug()<<"删除了";
            return true;
        }
    }else if (index.column() == 10 && event->type() == QEvent::MouseButtonRelease) {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
        if (option.rect.contains(mouseEvent->pos())) {
            emit editButtonClicked(index.row());
            qDebug()<<"编辑了";
            return true;
        }
    }else if (index.column() == 11 && event->type() == QEvent::MouseButtonRelease) {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
        if (option.rect.contains(mouseEvent->pos())) {
            emit checkDevice(index.row());
            qDebug()<<"查看了";
            return true;
        }
    }
    return QStyledItemDelegate::editorEvent(event, model, option, index);
}
